package com;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.*;

class Ticket{
    private int count=3;
    private Lock lock = new ReentrantLock();

    public void sale(){
        this.lock.lock();
        try {
            if(this.count>0){
                TimeUnit.SECONDS.sleep(1);
                System.out.println("【"+Thread.currentThread().getName()+"】买票，剩余票数："+this.count--);
            }else {
                System.err.println("【"+Thread.currentThread().getName()+"】票卖完了，明天再来");
            }
        }catch (Exception e){

        }finally {
            this.lock.unlock();
        }
    }
}
public class h7 {
    public static void main(String[] args)throws Exception {
        Ticket ticet=new Ticket();
        for (int x =0;x<5;x++){
            new Thread(()->{
                ticet.sale();
            },"售票员 - "+(x+1)).start();
        }
    }
}
